/********************************
 * ETK - Easy ToolKit
 * Copyright (C) 2007-2008 Ekinoks (scv.ekinoks@gmail.com)
 * All rights reserved.
 *
 *    This file is part of ETK.
 *
 *    Foobar is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    Foobar is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Foobar; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 ***********************************************************************************/

#include "StateModel.h"

#include "../State/State.h"
#include <cassert>

namespace etk
{
    StateModel StateModel::operator+ (const StateModel & s) const
    {
        StateModel tmp(0);

        return (tmp+=(*this)) += s;
    }

    StateModel& StateModel::operator+= (const StateModel & s) //non const       // Implementation !
    {
        state_ |= s.state_;
        return *this;
    }

    StateModel StateModel::operator- (const StateModel & s) const
    {
        StateModel s2(s.state_<<19);
        return (*this) + s2;
    }

    StateModel& StateModel::operator-= (const StateModel & s) //non const
    {
        StateModel s2(s.state_<<19);
        return (*this)+=s2;
    }

    bool StateModel::operator== (const State& state) const                  // Implementation !
    {
        assert(state.state_ != -1 && "ToolKit::updateEvent uncalled" );
        return ((state_ & state.state_) == state_);
    }

    bool StateModel::operator!= (const State& state) const                  // appelle de ==
    {
        return !((*this)==state);
    }

    bool StateModel::operator== (const StateModel& stateModel) const        // Implementation !
    {
        return state_ == stateModel.state_;
    }

    bool StateModel::operator!= (const StateModel& stateModel) const        // appelle de ==
    {
        return !((*this)==stateModel);
    }

    StateModel StateModel::onlyPressed() const
    {
        StateModel result( (state_<<19) >> 19 );

        return result;
    }

    StateModel StateModel::onlyUnpressed() const
    {
        StateModel result( (state_>>19) << 19 );

        return result;
    }

    StateModel StateModel::onlyLock() const
    {
        StateModel result( state_ & 0x197E032 );

        return result;
    }

    StateModel StateModel::onlyUnlock() const
    {
        StateModel result( state_ & 0xFE681FCD );

        return result;
    }

    StateModel StateModel::only() const
    {
        StateModel result( state_ );

        for(int i=0; i<13; ++i)
        {
            if( getBit(i) == getBit(i+19) )
                result.setBit(i+19);
        }

        return result;
    }

    const StateModel StateModel::ANY(0);
    const StateModel StateModel::SHIFT(1<<0);
    const StateModel StateModel::CAPS_LOCK(1<<1);
    const StateModel StateModel::CTRL(1<<2);
    const StateModel StateModel::ALT(1<<3);
    const StateModel StateModel::NUM_LOCK(1<<4);
    const StateModel StateModel::SCROLL_LOCK(1<<5);
    const StateModel StateModel::SUPER(1<<6);
    const StateModel StateModel::ALTGR(1<<7);
    const StateModel StateModel::BUTTON1(1<<8);
    const StateModel StateModel::BUTTON2(1<<9);
    const StateModel StateModel::BUTTON3(1<<10);
    const StateModel StateModel::BUTTON4(1<<11);
    const StateModel StateModel::BUTTON5(1<<12);
}

